package problem1143;

//1143.最长公共子序列
//https://leetcode.cn/problems/longest-common-subsequence/

class Solution {
    public int longestCommonSubsequence(String s1, String s2) {
        int n = s1.length();
        int m = s2.length();
        s1 = " " + s1;
        s2 = " " + s2;
        int[][] dp = new int[n+1][m+1];
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                if(s1.charAt(i) == s2.charAt(j)) {
                    dp[i][j] = Math.max(dp[i][j], dp[i-1][j-1] + 1);
                }else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[n][m];
    }
}

/*
dp[i][j]: 以s1的第i个字符串为结尾 和 s2 的第j个字符串为结尾 的最长公共子序列的长度
*/